#include "stdafx.h"
#include "./ConstantTreeNode.h"
#include "mocks.h"
#include "ComputationalTreeNodeVisitor.h"

using CollabRC::Bot::AI::ComputationalTreeNode;

/** {@inheritDoc} */
const ComputationalTreeNode& CollabRC::Bot::AI::ConstantTreeNode::GetRandomBranch( unsigned int depth ) const
{
    depth = depth;  // Avoid warnings for not used
    // It's impossible to go any deeper so this is what we must return
    return *this;
}


/** {@inheritDoc} */
unsigned int CollabRC::Bot::AI::ConstantTreeNode::GetBranchDepth() const
{
    // Always returns 1, because there is no further depth
    return 1;
}

/** {@inheritDoc} */
ComputationalTreeNode* CollabRC::Bot::AI::ConstantTreeNode::Clone() const
{
    // Reinstantiate self
    return new ConstantTreeNode(*this);
}

/** {@inheritDoc} */
ComputationalTreeNode* CollabRC::Bot::AI::ConstantTreeNode::Mutate(unsigned int depth) const
{
    depth = depth;  // Avoid warnings. Irrelevant, this is a depth of 1.

    // Modify the constant by +/- 25% or +/- 0.25

    // 0.25x > 0.25
    // x > 1

    float delta;
    if (m_value > 1 || m_value < -1)
        delta = 0.25 * m_value;
    else
        delta = 0.25;        

    // Randomize in either direction
    delta *= 2 * frand() - 1;

    return new ConstantTreeNode(m_value + delta);
}

/** {@inheritDoc} */
float CollabRC::Bot::AI::ConstantTreeNode::Compute() const
{
    return m_value;
}

/** {@inheritDoc} */
void CollabRC::Bot::AI::ConstantTreeNode::Accept(CollabRC::Bot::AI::ComputationalTreeNodeVisitor& visitor)
{
    visitor.Visit(*this);
}